#!/bin/sh

if [ $# -ne 0 ]; then
	echo "Usage: this is a empty shell."
	exit
fi

workdir=~/workshop/gitee/3place

cd $workdir

today=`date -I`

rm -f tmp.*.md
rm -f index_readcal.md 


find . | grep index_readcal.md |sed -e "s/index_readcal.md//g" >/tmp/tmp.md

echo "% 阅读书单@第三同读">index_readcal.md 

echo "% hbaolong@vip.qq.com">>index_readcal.md 

echo "" >>index_readcal.md

# echo "header"

commcode header >> index_readcal.md


for files in `cat /tmp/tmp.md`
do
	readmode="NO"

	if [ ! -e $files/config ]; then 
		continue
	fi
	
	source $files/config

	bookstartdate=`echo $startdate | sed -e "s/[年月]/-/g" |sed -e "s/日.*//g"`
	tmpstartdate=`daa +0 $bookstartdate  2>/dev/null | sed -e "s/[年月]/-/g" |sed -e "s/ /0/g"`

	bookenddate=`echo $enddate | sed -e "s/[年月]/-/g" |sed -e "s/日.*//g"`
	tmpenddate=`daa +0 $bookenddate  2>/dev/null |  sed -e "s/[年月]/-/g" |sed -e "s/ /0/g"`

	if [ $readmode = "COREAD" ]; then 
		echo $bookname " " $startdate " " $enddate " $url">> tmp.4.md
	elif [ $readmode = "QUICKVIEW" ]; then
		echo $bookname " " $startdate " " $enddate " $url">> tmp.5.md
	fi

	# 比较日期

	if [[ $today < $tmpstartdate ]]; then
		#echo "\033[31m" $bookname "还未开始" $bookstartdate "\033[0m"
		echo $bookname " " $startdate " " $enddate " $url">> tmp.1.md
	elif [[ $today > $tmpenddate ]]; then
		#echo "\033[31m" $bookname "已经于 " $bookenddate "结束" "\033[0m"
		echo $bookname " " $startdate " " $enddate " $url">> tmp.2.md
	else
		#echo $bookname "已经于 " $bookstartdate "开始"
		echo $bookname " " $startdate " " $enddate " $url">> tmp.3.md
	fi

done

echo "## 同读书单\n" >>index_readcal.md 
sort -k 2,2 -k 1,3 tmp.4.md |awk '{print "["$1"]("$4") : "$2 "-" $3 "\n"}' >>index_readcal.md

echo "## 速读书单\n" >>index_readcal.md 
sort -k 2,2 -k 1,3 tmp.5.md |awk '{print "["$1"]("$4") : "$2 "-" $3 "\n"}' >>index_readcal.md

echo "## 正在进行\n" >>index_readcal.md 
sort -k 2,2 -k 1,3 tmp.3.md |awk '{print "["$1"]("$4") : "$2 "-" $3 "\n"}' >>index_readcal.md

echo "## 即将开始\n" >>index_readcal.md 
sort -k 2,2 -k 1,3 tmp.1.md |awk '{print "["$1"]("$4") : "$2 "-" $3 "\n"}' >>index_readcal.md

echo "## 已经结束\n" >>index_readcal.md 
sort -k 2,2 -k 1,3 tmp.2.md |awk '{print "["$1"]("$4") : "$2 "-" $3 "\n"}' >>index_readcal.md


# echo "footer"

commcode footer >> index_readcal.md

rm -f tmp.1.md tmp.2.md tmp.3.md tmp.4.md tmp.5.md

pandoc -s index_readcal.md -o ~/workshop/hbaolong.github.io/index_readcal.html
